<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';

class Administrator_UserController extends Zend_Controller_Action {

    public function init() {

        $member = new CMember();

        if (!$member->isUser($this->_request->getModuleName(), $this->_request->getControllerName())) {

            $this->_redirect(MY_URL . '/administrator-auth-login');
        }
        
   		$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
				
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	
        	if (!in_array('5', $strarray)) {
        		exit('no priviage');
        	}
		}
    }

    public function preDispatch() {

        $this->getHelper('layout')->setLayout('framelayout');

        $this->view->addHelperPath(MY_ROOT . '/vhelpers');

        Zend_Controller_Action_HelperBroker::addPath(MY_ROOT . '/chelpers');

        //$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());
    }
    public function regAction(){
    		$begintime = $this->getRequest()->getParam('begintime');
    		if(empty($begintime)){
    			$begintime = date("Y-m-d",time()-24*3600);
    		}
			$start_time = $begintime;
    		$from = strtotime($begintime);
    		$endtime = $this->getRequest()->getParam('endtime');
    		if(empty($endtime)){
    			$endtime = date("Y-m-d",time());
    		}
    		$end_time = $endtime;
    		$to = strtotime($endtime);
    		$status = $this->getRequest()->getParam('flid');
    		$memberModel = new CMember();
    		$result = $memberModel->getRegOrder($from, $to, $status);
    		$this->view->assign('userArray',$result);
    		$this->view->assign('start_time',$start_time);
    		$this->view->assign('end_time',$end_time);
    		$this->view->assign('status',$status);
    	
    	
    }

    public function allAction() {

        $page = $this->_request->getParam("page", 1);

        $username = $this->_request->getParam("username");

        $state = $this->_request->getParam("state");

        $member = new CMember();

        $fieldArray = null;

        if ($username != '') {

            $fieldArray['username'] = $username;
        }

        if ($state != '') {

            $fieldArray['state'] = $state;
        }

        $userCounts = $member->getUserCounts($fieldArray);

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage($page, $userCounts, 5);

        $userArray = $member->getAllUsers($pageArray['from'], 5, $fieldArray);

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::divPages($pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH . "/administrator-user-all?username=$username&state=$state"); //生成分页字符串

        $this->view->assign("pageString", $pageString);

        $this->view->assign("pageArray", $pageArray);

        $this->view->assign('userArray', $userArray);

        $this->view->assign('username', $username);

        $this->view->assign('state', $state);
    }
    
    public function modAction()
    {
    	$uid = $this->getRequest()->getParam('uid');
    	$member = new CMember();
    	
    	$user = $member->getUserDetails(array('uid'=>$uid));
    	$this->view->userName = $user[0]['username'];
    	$this->view->uid = $uid;
    	if ($this->getRequest()->isPost()) {
    		$pass = $this->getRequest()->getParam('pass');
    		$passConfirm = $this->getRequest()->getParam('confirm_pass');
    		if ($pass != $passConfirm) {
    			$this->getHelper('viewRenderer')->setNoRender();
    			$this->view->showMsg('密码不同', '/administrator-user-mod?uid=' . $uid);
    			return;
    		}
    		$password = $this->getHelper('user')->encodePassword($pass,$user[0]['username'],$user[0]['reg_time'],$user[0]['reg_ip']);
    		if ($member->update(array('userpass'=>$password), 'uid = ' . intval($uid))) {
	    		$this->getHelper('viewRenderer')->setNoRender();
	    		$this->view->showMsg('修改成功', '/administrator-user-mod?uid=' . $uid);
    		}
    	}    
    }
	
    public function addscoreAction()
    {
    	$uid = $this->getRequest()->getParam('uid');
    	$member = new CMember();
    	
    	$user = $member->getUserDetails(array('uid'=>$uid));
    	
    	$this->view->userName = $user[0]['username'];
    	$this->view->integral = $user[0]['integral'];
    	
    	$score = new CScore();
    	$this->view->scoreDollar = $score->orderScore('score');
    	$this->view->uid = $uid;
    	
   		if ($this->getRequest()->isPost()) {
    		$addscore = $this->getRequest()->getParam('score');    		
    		$member->updateIntegral($addscore, $uid, 1);
    		$this->getHelper('viewRenderer')->setNoRender();
    		$this->view->showMsg('修改成功', '/administrator-user-addscore?uid=' . $uid);
    	}    	
    }
    
    public function adminlistAction() {

        $page = $this->_request->getParam("page", 1);

        $username = $this->_request->getParam("username");

        $state = $this->_request->getParam("state");

        $member = new CMember();

        $fieldArray = null;

        $fieldArray['member_type'] = "administrator";
        if ($username != '') {

            $fieldArray['username'] = $username;
        }

        if ($state != '') {

            $fieldArray['state'] = $state;
        }

        $userCounts = $member->getUserCounts($fieldArray);

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage($page, $userCounts, 5);

        $userArray = $member->getAllUsers($pageArray['from'], 5, $fieldArray);

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::divPages($pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH . "/administrator-user-all?username=$username&state=$state"); //生成分页字符串

        $this->view->assign("pageString", $pageString);

        $this->view->assign("pageArray", $pageArray);

        $this->view->assign('userArray', $userArray);

        $this->view->assign('username', $username);

        $this->view->assign('state', $state);
    }

    public function usertypeAction() {

        $type = $this->_request->getParam("type");

        $page = $this->_request->getParam("page", 1);

        $username = $this->_request->getParam("username");

        $state = $this->_request->getParam("state");

        $fieldArray = null;

        $fieldArray['member_type'] = $type;

        if ($username != '') {

            $fieldArray['username'] = $username;
        }

        if ($state != '') {

            $fieldArray['state'] = $state;
        }

        $member = new CMember();

        $userCounts = $member->getUserCounts($fieldArray);

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage($page, $userCounts, 5);

        $userArray = $member->getAllUsers($pageArray['from'], 5, $fieldArray);

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::divPages($pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH . "/administrator-user-usertype?type=" . $type . "&username=$username&state=$state"); //生成分页字符串

        $this->view->assign("pageString", $pageString);

        $this->view->assign("pageArray", $pageArray);

        $this->view->assign('userArray', $userArray);

        $this->view->assign('username', $username);

        $this->view->assign('state', $state);

        $this->render('all');
    }

    public function permitAction() {

        $this->_helper->viewRenderer->setNoRender();

        $thisArray = $this->_request->getParams();

        $permission = $thisArray['set'];

        $member = new CMember();

        for ($i = 0; $i < count($thisArray['itemArray']); $i++) {

            $member->updateUserDetail(
                    array('state' => $permission), array('uid' => $thisArray['itemArray'][$i])
            );
        }

        $this->view->showMsg('修改成功!');
    }

    public function moreAction() {

        $type = $this->_request->getParam("type");

        $this->view->assign('type', $type);

        $uid = $this->_request->getParam("uid");

        $this->view->assign('uid', $uid);

        $myaction = $this->_request->getParam("myaction", 'intro');

        $this->view->assign('myaction', $myaction);

        if ($type == 'seller') {

            if ($myaction == 'intro') {

                $seller = new CSeller();

                $sellerArray = $seller->getUserDetails(array('uid' => $uid));

                $this->view->assign('sellerArray', $sellerArray);

                $bank = new CBank();

                $bankArray = $bank->getAllBanks($uid);

                $this->view->assign('bankArray', $bankArray);
            } else if ($myaction == 'contact') {

                $seller = new CSeller();

                $sellerArray = $seller->getUserDetailsUnion(array(R_PRE . 'sellers.uid' => $uid));

                $this->view->assign('sellerArray', $sellerArray);
            } else if ($myaction == 'account') {

                $this->sellerAccount($uid);
            } else if ($myaction == 'virtual') {

                $member = new CMember();

                $memberArray = $member->getUserDetails(array('uid' => $uid));

                $this->view->assign('memberArray', $memberArray);
            }
        } else if ($type == 'buyer') {

            if ($myaction == 'intro') {

                $business = new CBusiness();

                $businessArray = $business->getBusinesses();

                $this->view->assign('businessArray', $businessArray);

                $buyer = new CBuyer();

                $buyerArray = $buyer->getUserDetails(array('uid' => $uid));

                $this->view->assign('buyerArray', $buyerArray);

                $btype = new CBtype();

                $btypeArray = $btype->getBtypes();

                $this->view->assign('btypeArray', $btypeArray);

                $location = new CFenlei('location');

                $locationArray = $location->getAllFenlei();

                $selectString = $location->getAllSelect($locationArray, $buyerArray[0]['location_id'], 'locationFl.getChildFenlei(this);');

                $this->view->assign('selectString', $selectString);

                $address = new CAddress();

                $addressArray = $address->getAllAddresses($uid);

                $this->view->assign('addressArray', $addressArray);

                $bank = new CBank();

                $bankArray = $bank->getAllBanks($uid);

                $this->view->assign('bankArray', $bankArray);
            } else if ($myaction == 'contact') {

                $buyer = new CBuyer();

                $buyerArray = $buyer->getUserDetailsUnion(array(R_PRE . 'buyers.uid' => $uid));

                $this->view->assign('buyerArray', $buyerArray);
            } else if ($myaction == 'account') {

                $this->buyerAccount($uid);
            } else if ($myaction == 'virtual') {

                $member = new CMember();

                $memberArray = $member->getUserDetails(array('uid' => $uid));

                $this->view->assign('memberArray', $memberArray);
            }
        } else if ($type == 'administrator') {

            $this->_forward('recentchat');

            return true;
        }

        $this->render('more-' . $type . '-' . $myaction);

        return true;
    }

    public function addadminAction() {
        
    }

    public function updateadminAction() {
        $type = $this->_request->getParam("type");

        $this->view->assign('type', $type);

        $uid = $this->_request->getParam("uid");

        $this->view->assign('uid', $uid);

        $member = new CMember();

        $memberArray = $member->getUserDetails(array('uid' => $uid));

        $this->view->assign('memberArray', $memberArray);
    }

    public function doupdateadminAction() {

        $this->_helper->viewRenderer->setNoRender();

        if (!$this->getRequest()->isPost()) {

            $this->view->showMsg("发生未知错误!");

            return false;
        }

        Zend_Session::start();

        $paramArray = $this->_request->getParams();

        $filters = array(
            '*' => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);

        $menuflag = $input->menuflag;

        $menuflagstr = "";
        if ($menuflag) {
            foreach ($menuflag as $v) {
                if ($menuflagstr == "") {
                    $menuflagstr = "$v";
                } else {
                    $menuflagstr.=",$v";
                }
            }
        }

        $member = new CMember();

        $username = $input->username;

        $userpass = $input->userpass;

        $time = time();

        $ip = $this->getHelper('User')->getIp();

        if ($input->userpass) {
            $md5Password = $this->getHelper('user')->encodePassword($userpass, $username, $time, $ip);

            $member->updateUserDetail(
                    array(
                'userpass' => $md5Password,
                'reg_time' => $time,
                'reg_ip' => $ip,
                    ), array('uid' => $input->uid)
            );
        }

        $member->updateUserDetail(
                array(
            'menuflag' => $menuflagstr,
            'email' => $input->email,
                ), array('uid' => $input->uid)
        );

        $this->view->showMsg('修改成功!');
    }

    

    public function doaddadminAction() {

        $this->_helper->viewRenderer->setNoRender();

        if (!$this->getRequest()->isPost()) {

            $this->view->showMsg("发生未知错误!");

            return false;
        }

        Zend_Session::start();

        $paramArray = $this->_request->getParams();

        $filters = array(
            '*' => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);

        $member = new CMember();

        $username = $input->username;

        $userArray = $member->getUserDetails(array('username' => $username));

        if (count($userArray) > 0) {

            $this->view->showMsg('The nick name you entered has been registered by others!');

            return false;
        }

        $userpass = $input->userpass;

        $time = time();

        $ip = $this->getHelper('User')->getIp();

        $md5Password = $this->getHelper('user')->encodePassword($userpass, $username, $time, $ip);

        $commonData = array(
            'username' => $username,
            'userpass' => $md5Password,
            'email' => $input->email,
            'reg_time' => $time,
            'reg_ip' => $ip,
            'member_type' => 'administrator',
            'is_enterprice' => '1',
        );

        $uid = $member->create($commonData);

        $this->view->showMsg('添加成功!');
    }

    public function sellerAccount($uid) {

        $order = new COrder();

        $orderArray = $order->getAllOrders(0, 0, 0, '', $uid);

        $disputeAccount = 0;

        $successAcount = 0;

        $cdsAcount = 0;

        foreach ($orderArray as $value) {

            if ($value['flid'] == '6') {

                $disputeAccount+= ( $value['money'] - $value['shipping_cost']) * $value['discount'];
            } else if ($value['flid'] == '9') {

                $successAcount+= ( $value['money'] - $value['shipping_cost']) * $value['discount'];

                $cdsAcount+= ( $value['money'] - $value['shipping_cost']) * (SELL_RATE - 1);
            }
        }

        $disputeAccount = round($disputeAccount, 2);

        $successAcount = round($successAcount, 2);

        $cdsAcount = round($cdsAcount, 2);

        $sellAccount = $disputeAccount + $successAcount + $cdsAcount;

        $this->view->assign('sellAccount', $sellAccount);

        $this->view->assign('disputeAccount', $disputeAccount);

        $this->view->assign('successAcount', $successAcount);

        $this->view->assign('cdsAcount', $cdsAcount);

        $account = new CAccount();

        $hasReceiveAccount = 0;

        $accountArray = $account->getAllAccounts(0, 0, "to_uid='" . $uid . "' AND " . R_PRE . "accounts.flid='2'");

        foreach ($accountArray as $value) {

            $hasReceiveAccount+=$value['has_paid'];
        }

        $this->view->assign('hasReceiveAccount', $hasReceiveAccount);


        $hasRequireAccount = 0;

        $accountArray = $account->getAllAccounts(0, 0, "from_uid='" . $uid . "' AND " . R_PRE . "accounts.flid='4'");

        foreach ($accountArray as $value) {

            $hasRequireAccount+=$value['should_paid'];
        }

        $this->view->assign('hasRequireAccount', $hasRequireAccount);

        $canRequireAccount = 0;

        $canRequireAccount = $successAcount - $hasRequireAccount;

        $this->view->assign('canRequireAccount', $canRequireAccount);

        $waitingRequireAccount = $hasRequireAccount - $hasReceiveAccount;

        $this->view->assign('waitingRequireAccount', $waitingRequireAccount);
    }

    public function buyerAccount($uid) {

        $order = new COrder();

        $orderArray = $order->getAllOrders(0, 0, $uid);

        $disputeAccount = 0;

        foreach ($orderArray as $value) {

            if ($value['flid'] == '6') {

                $disputeAccount+= ( $value['money'] - $value['shipping_cost']) * $value['discount'];
            }
        }

        $disputeAccount = round($disputeAccount, 2);

        $this->view->assign('disputeAccount', $disputeAccount);

        $account = new CAccount();

        $accountArray = $account->getAllAccounts(0, 0, "from_uid='" . $uid . "'");

        $buyAccount = 0;

        $receiveAccount = 0;

        $buyBalance = 0;

        $actualReceiveAccount = 0;

        $actualRequireAccount = 0;

        foreach ($accountArray as $value) {

            if ($value['flid'] == '1') {

                $buyAccount+=$value['has_paid'];
            } else if ($value['flid'] == '2') {

                $receiveAccount+=$value['has_paid'];
            } else if ($value['flid'] == '5') {

                $actualReceiveAccount+=$value['has_paid'];

                $actualRequireAccount+=$value['should_paid'];
            }
        }

        $buyBalance = $buyAccount - $actualReceiveAccount;

        $this->view->assign('buyAccount', $buyAccount);

        $this->view->assign('receiveAccount', $receiveAccount);

        $this->view->assign('actualRequireAccount', $actualRequireAccount);

        $this->view->assign('actualReceiveAccount', $actualReceiveAccount);

        $this->view->assign('buyBalance', $buyBalance);

        $member = new CMember();

        $memberArray = $member->getUserDetails(array('uid' => $uid));

        $this->view->assign('memberArray', $memberArray);
    }

    public function recentchatAction() {

        $this->view->assign('myaction', 'recentchat');

        $type = $this->_request->getParam("type");

        $this->view->assign('type', $type);

        $recentUserArray = array();

        $uid = $this->_request->getParam("uid");

        $this->view->assign('uid', $uid);

        $file = floor($uid / 100);

        $path = MY_ROOT . '/html/upload/recent_chat/' . $file . '/' . $uid . '.txt';

        if (file_exists($path)) {

            $userString = file_get_contents($path);

            $recentUserArray = json_decode($userString, true);
        }

        $this->view->assign('recentUserArray', $recentUserArray);
    }

    public function chatrecordAction() {

        $this->view->assign('myaction', 'chatrecord');
    }

    public function postAction() {

        //$this->_helper->viewRenderer->setNoRender();
        //$this->getHelper ('layout')->disableLayout ();
    }
   
    	

}
